Back to BASIC
By Grant Kwai
Copyright (c) 1992 Apple Users' Group, Sydney
Republished from Applecations, a publication of the Apple Users' Group, Sydney, Australia.
FOR-NEXT. The use of these statements create what is called a loop. Instructions within a FOR-NEXT loop are executed a specified number of times. Here is a simple FOR-NEXT loop:
10 PRINT "A FOR-NEXT EXAMPLE"
20 FOR LOOP=1 TO 5
30 PRINT LOOP
40 NEXT LOOP
50 PRINT "THE END"
Output:
-------------------
A FOR-NEXT EXAMPLE
1
2
3
4
5
THE END
-------------------
When you RUN this program it will give the output of the numbers 1 to 5 consecutively. How does this work?
The variable LOOP (in our program) can store a numeric value.
FOR LOOP=1 to 5
This defines the upper and lower limits of our variable LOOP. We start by giving the variable LOOP an initial value of 1. Now this has been set, it will move onto the next line, line 30:
PRINT LOOP
The PRINT command will output the value of LOOP to the screen. Since from line 20, LOOP was given a value of 1, 1 is printed to our screen. Now that this line has been completed, the program moves onto line 40:
NEXT LOOP
The word NEXT informs the computer that it has finished with the current value of LOOP. Processing returns to the FOR statement in line 20. Since a value of 1 has already been assigned, and line 40 requires the next value, LOOP will now be assigned the value 2.
Processing continues until LOOP contains the value 5. Since line 20 limits the value of LOOP to be from 1 to 5, it can not contain 6. This signals the end of the loop. Processing now skips the entire FOR-NEXT loop and continues at line 50.
Here is another program:
10 A=10 :B=20
20 FOR X=A TO B
30 PRINT X
40 NEXT X
Output:
-------------------
10
11
.
.
.
20
--------------------
You will see that the output will be the numbers 10 to 20. With the FOR statement, you need not give it number values directly. You can pass on variables which have a number value to the loop.
A further command to add to a FOR-NEXT loop is STEP. A program using the step command is:
10 FOR A= 1 TO 41 STEP 10
20 PRINT A
30 NEXT A
Output:
-------------------
1
11
21
31
41
-------------------
Why does it give this? As you can see, it prints out the number with 10 added to it. Why does it start at 1?
It starts at 1 because the variable A is assigned the value 1. It moves onto line 20 which prints out the value of A. Line 30 increments the variable A.
This is where the STEP statement comes in. Unlike in the first loop example it will not increase A by 1. Instead, STEP defines the increment value, in this case 10. We note then, that the next number to be printed is 11, then 21 and so forth.
It will stop executing, when the STEP command tries to go beyond the limit of the LOOP. You will also note that if line 10 was to read:
10 FOR LOOP=1 TO 40 STEP 10
the number 41 would not be printed. The loop would end after printing 31.
STEP can also be used to count backwards. That is, for example counting from 100 back to 1. Try this program:
10 FOR A=100 TO 1 STEP -1
20 PRINT A
30 NEXT A
The output from the above program will be the numbers 100 to 1 printed in reverse order. Since the STEP command says -1, it means to take a step backwards.
What about this program?
10 FOR A=1 TO 100 STEP -1
20 PRINT A
30 NEXT A
No, this program will not print from 100 to 1. All it will print is 1. Why?
In line 10, the FOR statement limits the valid values of A to be from 1 to 100. Since the first value of A is 1, this value will go through the loop, hence being printed.
When the variable is decremented the limits are exceeded and the loop is terminated.
The golden rule here is, if you are counting backwards, you must also reverse the order on the FOR loop to also go from highest value to lowest value.
As a final example, we shall investigate nested loops. That is, loops within another loop.
10 FOR A=1 TO 5
20 FOR B=10 TO 15
30 PRINT B
40 NEXT B
50 PRINT A
60 NEXT A
If you thought the output of this program would be to print the numbers 10 to 15 then 1 to 5, you are wrong.
You were correct in assuming that the second loop (lines 20-40) would be executed first (hence the numbers 10-15), however 1 to 5 are not printed consecutively.
Lets go through this program step by step.
Line 10 sets up the variable A to have a value between 1 to 5. It is the start of loop A.
Line 20 assigns the variable B to have a value between 10 to 15. This is loop B. Loop B, which consists of lines 20-40 are then executed as per the above examples. The output therefore are the numbers 10 to 15.
Now that this loop has been run, it moves onto the next line in the program, which is line 50.
Line 50 tells the computer to print the value of A, which is still set at 1. It then moves onto line 60. Going back to line 10, the next value of A is possible, so now A=2. Since loop B is nested inside loop A, it will be executed again. Line 20 says that FOR B=10 to 15. This re-initialises B again to start back at 10. Hence loop B is run again, and the numbers 10 to 15 are printed.
This will continue for each value of A as long as loop A is valid. Hence, the final output will be:
-----------------------
10
11
...
15
1
10
...
15
2
10
...
15
...
...
5
------------------------
Thus we come to an end of this month's Back to Basic. More BASIC commands shall be explained in coming months.

THIS CONTENT COPYRIGHT © 2007, APPLE MACINTOSH USERS' GROUP, SYDNEY
Permission has been obtained to make this material available on the Internet.

Permission is hereby granted for non-profit user groups to republish this content.
PLEASE CREDIT THE AUTHOR AND THE SOURCE: Applecations, publication of the Apple Users' Group, Sydney, Australia

THIS PAGE COPYRIGHT © 2007, ANDREW ROUGHAN